package net.helix.core;

public class Bounds {
	private float x1 = 0;
	private float x2 = 0;
	private float y1 = 0;
	private float y2 = 0;
	private float z1 = 0;
	private float z2 = 0;
	
	private float xa = 0;
	private float ya = 0;
	private float za = 0;
	private float c = 0;
	
	private boolean first = true;
	
	float arr[][] = new float[8][3];
	public Bounds(){
		
	}
	public void clear(){
		x1 = 0;
		y1 = 0;
		z1 = 0;
		x2 = 0;
		y2 = 0;
		z2 = 0;
		
		xa = 0;
		ya = 0;
		za = 0;
		c = 0;
		
		first = true;
	}
	public void consider(float x, float y, float z){
		if(first){
			x1 = x2 = x;
			y1 = y2 = y;
			z1 = z2 = z;
		}else{
			x1 = Math.min(x, x1);
			x2 = Math.max(x, x2);
			
			y1 = Math.min(y, y1);
			y2 = Math.max(y, y2);
			
			z1 = Math.min(z, z1);
			z2 = Math.max(z, z2);
		}
		
		xa += x;
		ya += y;
		za += z;
		c += 1;
		
		first = false;
	}
	public void consider(Bounds b){
		if(first){
			x1 = b.x1;
			y1 = b.y1;
			z1 = b.z1;
			
			x2 = b.x2;
			y2 = b.y2;
			z2 = b.z2;
		}else{
			x1 = Math.min(b.x1, x1);
			x2 = Math.max(b.x2, x2);
			
			y1 = Math.min(b.y1, y1);
			y2 = Math.max(b.y2, y2);
			
			z1 = Math.min(b.z1, z1);
			z2 = Math.max(b.z2, z2);
		}
		
		xa += b.xa;
		ya += b.ya;
		za += b.za;
		c += b.c;
		
		first = false;
	}
	public float getWidth(){
		return x2 - x1;
	}
	public float getHeight(){
		return y2 - y1;
	}
	public float getDepth(){
		return z2 - z1;
	}
	
	public float getWidthIncludeCenter(){
		return Math.max(x2, 0) - Math.min(x1, 0);
	}
	public float getHeightIncludeCenter(){
		return Math.max(y2, 0) - Math.min(y1, 0);
	}
	public float getDepthIncludeCenter(){
		return Math.max(z2, 0) - Math.min(z1, 0);
	}
	public float[][] getBoundsAsArray(){
		arr[0][0] = x1;
		arr[0][1] = y1;
		arr[0][2] = z1;
		
		arr[1][0] = x2;
		arr[1][1] = y1;
		arr[1][2] = z1;
		
		arr[2][0] = x1;
		arr[2][1] = y2;
		arr[2][2] = z1;
		
		arr[3][0] = x2;
		arr[3][1] = y2;
		arr[3][2] = z1;
		
		arr[4][0] = x1;
		arr[4][1] = y1;
		arr[4][2] = z2;
		
		arr[5][0] = x2;
		arr[5][1] = y1;
		arr[5][2] = z2;
		
		arr[6][0] = x1;
		arr[6][1] = y2;
		arr[6][2] = z2;
		
		arr[7][0] = x2;
		arr[7][1] = y2;
		arr[7][2] = z2;
		
		return arr;
	}
	public float getMeanX(){
		if(c < 1) return 0;
		return xa/c;
	}
	public float getMeanY(){
		if(c < 1) return 0;
		return ya/c;
	}
	public float getMeanZ(){
		if(c < 1) return 0;
		return za/c;
	}
	public float getMiddleX(){
		return (x2+x1)/2;
	}
	public float getMiddleY(){
		return (y2+y1)/2;
	}
	public float getMiddleZ(){
		return (z2+z1)/2;
	}
	
}
